package com.sy.simpleegg.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.sy.simpleegg.bean.PayTypeData
import com.sy.simpleegg.databinding.ItemPayTypeBinding
import com.sy.simpleegg.ext.onClick

class PayTypeAdapter(
    private var mContext: Context,
    private var payTypeData: MutableList<PayTypeData>
) : RecyclerView.Adapter<PayTypeAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PayTypeAdapter.ViewHolder {
        val binding = ItemPayTypeBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("NewApi", "SetTextI18n", "UseCompatLoadingForDrawables")
    override fun onBindViewHolder(holder: PayTypeAdapter.ViewHolder, position: Int) {
        val payType: PayTypeData = payTypeData[position]

        if (payType.icon.isNotEmpty()) {
            holder.ivIcon.visibility = View.VISIBLE
            Glide.with(holder.itemView)
                .load(payType.icon)
                .fitCenter()
                .into(holder.ivIcon)
        }else{
            holder.ivIcon.visibility = View.GONE
        }

        holder.tvPayType.text = payType.name

        holder.btnPayType.onClick {
            mOnClickListener?.onClick(payType)
        }
    }

    override fun getItemCount(): Int {
        return payTypeData.size
    }

    inner class ViewHolder(binding: ItemPayTypeBinding) : RecyclerView.ViewHolder(binding.root) {
        val btnPayType: RelativeLayout = binding.rlBtn
        val ivIcon: ImageView = binding.ivIcon
        val tvPayType: TextView = binding.tvPayType
    }

    interface OnItemClickListener {
        fun onClick(payTypaData: PayTypeData?)
    }

    private var mOnClickListener: OnItemClickListener? = null
    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnClickListener = onItemClickListener
    }
}